<?php

namespace Proyecto\MainBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Proyecto\MainBundle\Entity\CategoriaItem;
use Proyecto\MainBundle\Form\CategoriaItemType;

use Symfony\Component\HttpFoundation\Response;

/**
 * CategoriaItem controller.
 *
 * @Route("/admin/categoriaitem")
 */
class CategoriaItemController extends Controller
{
    /**
     * Lists all CategoriaItem entities.
     *
     * @Route("/", name="categoriaitem")
     * @Method("GET")
     * @Template()
     */
    public function indexAction()
    {
        $em = $this->getDoctrine()->getManager();
        
        $categorias = $em->getRepository('ProyectoMainBundle:CategoriaItem')->findAll();
        
           return array(
            'entities' => $categorias,
        ); 
    }
    
    /**
     * Lists all CategoriaItem entities in json format.
     *
     * @Route("/categorias_json", name="categorias_json", options={"expose"=true})
     * @Method("GET")
     * @Template()
     */
    public function listJsonAction()
    {
        $em = $this->getDoctrine()->getManager();
        
        $categorias = $em->getRepository('ProyectoMainBundle:CategoriaItem')->findAll();
        $respuesta="[";
          if ($categorias){
		foreach ($categorias as $categoria){
                  $respuesta.="{'id_categoria':'".$categoria->getId()."','descripcion':'".$categoria->getDescripcion()."'},";
		}
		$respuesta=trim($respuesta,',');
		$respuesta.="]";
	  }		
          else{
	    $respuesta= "{'cantidad':'0'}";
          }
          $response = new Response();
          $response->setContent($respuesta);
          return $response;
    }


    public function createAction(Request $request)
    {
        $entity  = new CategoriaItem();
        $form = $this->createForm(new CategoriaItemType(), $entity);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('categoriaitem_show', array('id' => $entity->getId())));
        }

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    public function newAction()
    {
        $entity = new CategoriaItem();
        $form   = $this->createForm(new CategoriaItemType(), $entity);

        return array(
            'entity' => $entity,
            'form'   => $form->createView(),
        );
    }

    public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ProyectoMainBundle:CategoriaItem')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find CategoriaItem entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }


    public function editAction($id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ProyectoMainBundle:CategoriaItem')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find CategoriaItem entity.');
        }

        $editForm = $this->createForm(new CategoriaItemType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }

     public function updateAction(Request $request, $id)
    {
        $em = $this->getDoctrine()->getManager();

        $entity = $em->getRepository('ProyectoMainBundle:CategoriaItem')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find CategoriaItem entity.');
        }

        $deleteForm = $this->createDeleteForm($id);
        $editForm = $this->createForm(new CategoriaItemType(), $entity);
        $editForm->bind($request);

        if ($editForm->isValid()) {
            $em->persist($entity);
            $em->flush();

            return $this->redirect($this->generateUrl('categoriaitem_edit', array('id' => $id)));
        }

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }


    public function deleteAction(Request $request, $id)
    {
        $form = $this->createDeleteForm($id);
        $form->bind($request);

        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            $entity = $em->getRepository('ProyectoMainBundle:CategoriaItem')->find($id);

            if (!$entity) {
                throw $this->createNotFoundException('Unable to find CategoriaItem entity.');
            }

            $em->remove($entity);
            $em->flush();
        }

        return $this->redirect($this->generateUrl('categoriaitem'));
    }

    /**
     * Creates a form to delete a CategoriaItem entity by id.
     *
     * @param mixed $id The entity id
     *
     * @return Symfony\Component\Form\Form The form
     */
    private function createDeleteForm($id)
    {
        return $this->createFormBuilder(array('id' => $id))
            ->add('id', 'hidden')
            ->getForm()
        ;
    }
    
    
       

    
    
}
